package com.yw.mq.example.m03_order;

import cn.hutool.core.date.DateUtil;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.*;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

import static com.yw.mq.core.MqConstant.DEFAULT_CONSUMER_GROUP;
import static com.yw.mq.core.MqConstant.NAME_SERVER_ADDR;

public class Consumer {
    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(DEFAULT_CONSUMER_GROUP);

        consumer.setNamesrvAddr(NAME_SERVER_ADDR);

        consumer.subscribe("order_topic", "*");

        consumer.setConsumeThreadMax(20);
        consumer.setConsumeThreadMin(20);
        consumer.setConsumeMessageBatchMaxSize(5);

        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("收到顺序消息：" + new String(messageExt.getBody()));
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });


        consumer.start();
        System.out.println("消费者启动成功...");
    }
}
